package jaux.tank.api.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import javax.validation.constraints.NotEmpty;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jaux.tank.bean.constant.factory.PageFactory;
import jaux.tank.bean.dto.EntityExtensionDto;
import jaux.tank.bean.dto.PageRequestModel;
import jaux.tank.bean.entity.system.EntityExtension;
import jaux.tank.bean.entity.system.EntityExtension.Fields;
import jaux.tank.bean.vo.EntityQueryCondition;
import jaux.tank.bean.vo.front.JSONRet;
import jaux.tank.bean.vo.front.JSONRets;
import jaux.tank.service.system.EntityExtensionService;
import jaux.tank.utils.factory.Page;

@RestController
@RequestMapping("/api/entityExtension")
@Api("实体扩展属性web服务接口")
public class EntityExtensionController {

	@Autowired
	private EntityExtensionService entityExtensionService;


//	@GetMapping("/")
//	@ApiOperation("查询所有实体扩展属性")
//	public JSONRet<List<EntityExtension>> queryAll() {
//		return JSONRets.success(entityExtensionRepo.findAll());
//	}
	@GetMapping("/query")
	@ApiOperation("分页查询实体扩展属性")
	public JSONRet<Page<EntityExtension>> query(
			@ModelAttribute(binding = false) PageRequestModel pageModel
			, @RequestParam(required = false) String entity
			, @RequestParam(required = false) String entityId
			, @RequestParam(required = false) String name){
		Page<EntityExtension> page = entityExtensionService.query(Arrays.asList(
				new EntityQueryCondition(Fields.name, name)
				, new EntityQueryCondition(Fields.entity, entity)
				, new EntityQueryCondition(Fields.entityId, entityId)
				)
				, new PageFactory<EntityExtension>().defaultPage());
		return JSONRets.success(page);
	}
	@GetMapping("/queryByEntityId")
	@ApiOperation("查询实体扩展属性")
	public JSONRet<List<EntityExtension>> queryByEntityId(
			@NotEmpty @RequestParam String entity
			, @NotEmpty @RequestParam String entityId
			, @RequestParam(required = false) String name){
		return JSONRets.success(entityExtensionService.queryEntityExtensions(entity
				, entityId
				, name));
	}

	@GetMapping("/get")
	@ApiOperation("查询单个实体扩展属性")
	public JSONRet<EntityExtension> get(@RequestParam String id) {
		Optional<EntityExtension> opt =  entityExtensionService.get(id);
		if (opt.isPresent())
			return JSONRets.success(opt.get());
		return JSONRets.failure("object not exists");
	}

	@PostMapping("/add")
	@ApiOperation("添加新的实体扩展属性")
	public JSONRet<EntityExtension> add(@RequestBody EntityExtensionDto entityExtension) {
		return JSONRets.success(entityExtensionService.add(entityExtension.toEntity()));
	}

	@DeleteMapping("/delete")
	@ApiOperation("删除实体扩展属性")
	public JSONRet<String> delete(@RequestParam String id) {
		entityExtensionService.remove(id);
		return JSONRets.success(id);
	}
	
	@DeleteMapping("/deleteByEntityId")
	@ApiOperation("删除实体扩展属性")
	public JSONRet<String> delete(@RequestParam String entity, @RequestParam String entityId) {
		entityExtensionService.removeByEntityId(entity, entityId);
		return JSONRets.success();
	}

	@PutMapping("/update")
	@ApiOperation("修改实体扩展属性")
	public JSONRet<EntityExtension> update(@RequestBody EntityExtensionDto entityExtension) {
		if (StringUtils.isNotBlank(entityExtension.getId()))
			return JSONRets.success(entityExtensionService.update(entityExtension.toEntity()));
		return JSONRets.failure("id not exists");
	}

}
